import logging
from datetime import datetime
from .task_monitor import monitor_task

import requests

logger = logging.getLogger(__name__)

def simple_task():
    """简单定义任务"""
    logger.info(f"简单任务执行时：{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    print(f"简单任务执行时：{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")

def task_with_params(name: str, count: int):
    """带参数的任务"""
    logger.info(f"带参数任务执行：{name},次数：{count}")
    print(f"带参数任务执行：{name},次数：{count}")


def log_task(message: str="定时任务执行"):
    """日志任务"""
    logger.info(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}]：{message}")
    print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}]：{message}")

@monitor_task
def http_request_task(url: str, method: str = "GET", **kwargs):
    """HTTP请求任务"""
    try:
        if method.upper() == "GET":
            response = requests.get(url,timeout=10)
        elif method.upper() == "POST":
            response = requests.post(url,timeout=10)
        else:
            raise ValueError("不支持的HTTP方法")

        logger.info(f"HTTP请求任务执行：{url},状态码：{response.status_code}")
        return {"status_code": response.status_code, "status": "success"}
    except Exception as e:
        logger.error(f"HTTP请求任务执行失败：{url},错误信息：{str(e)}")
        return {"status": 500, "data": str(e)}
@monitor_task
def data_processing_task(data: dict, **kwargs):
    """数据处理任务"""
    logger.info(f"数据处理任务执行：{data}")
    # 模拟数据处理
    processed_data = {
        "input": data,
        "processed_at": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
        "result": f"处理了{len(str(data))} 个字符"
    }
    logger.info(f"处理结果：{processed_data}")
    return processed_data

def database_cleanup_task(days: int=30, **kwargs):
    """数据库清理任务"""
    logger.info(f"数据库清理任务执行：{days} 天前的数据")
    # 这里可以添加实际数据库清理逻辑

    logger.info("数据库清理完成")
    return {"status": "success", "message": "数据库清理完成"}

